package com.sfac.springBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.sfac.springBoot.modules.account.entity.User;
import com.sfac.springBoot.modules.account.service.UserService;
import com.sfac.springBoot.modules.common.vo.Result;
import com.sfac.springBoot.modules.common.vo.Search;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping(value = "/login",consumes = "application/json")
    @ResponseBody
    public Result<User> login(@RequestBody User user){
        return userService.login(user);
    }
    @GetMapping(value = "/user/{id}")
    @ResponseBody
    public User login(@PathVariable int id){
        return userService.getUserById(id);
    }

    @PostMapping(value = "/register",consumes = "application/json")
    @ResponseBody
    public Result<User> register(@RequestBody User user){
        return userService.register(user);
    }

    @PostMapping(value = "/users",consumes = "application/json")
    @ResponseBody
    public PageInfo<User> loadAllUser(@RequestBody Search Search){
        return userService.loadAllUsers(Search);
    }

    @PostMapping(value = "/user",consumes = "application/json")
    @ResponseBody
    public Result<User> addUser(@RequestBody User user){
        return userService.register(user);
    }
    @PutMapping(value = "/user",consumes = "application/json")
    @ResponseBody
    public Result<User> updateUser(@RequestBody User user){
        return userService.update(user);
    }

    @DeleteMapping(value = "/user/{id}")
    @RequiresPermissions(value={"/api/user/{id}"})
    @ResponseBody
    public Result<User> deleteUser(@PathVariable int id){
        return userService.deleteUserById(id);
    }
}
